package com.example.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "全局统一返回结果")
public class Result {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Object data;

    public Result() {
    }

    //请求成功返回
    public static Result ok() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }

    //创建成功返回
    public static Result created() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.CREATED.getSuccess());
        r.setCode(ResultCodeEnum.CREATED.getCode());
        r.setMessage(ResultCodeEnum.CREATED.getMessage());
        return r;
    }

    //删除成功返回
    public static Result deleted() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.DELETED.getSuccess());
        r.setCode(ResultCodeEnum.DELETED.getCode());
        r.setMessage(ResultCodeEnum.DELETED.getMessage());
        return r;
    }

    //修改成功返回
    public static Result update() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.UPDATED.getSuccess());
        r.setCode(ResultCodeEnum.UPDATED.getCode());
        r.setMessage(ResultCodeEnum.UPDATED.getMessage());
        return r;
    }

    //请求失败返回
    public static Result error() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.ERROR.getSuccess());
        r.setCode(ResultCodeEnum.ERROR.getCode());
        r.setMessage(ResultCodeEnum.ERROR.getMessage());
        return r;
    }

    //创建失败返回
    public static Result unCreated() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.UNCREATED.getSuccess());
        r.setCode(ResultCodeEnum.UNCREATED.getCode());
        r.setMessage(ResultCodeEnum.UNCREATED.getMessage());
        return r;
    }

    //删除失败返回
    public static Result unDeleted() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.UNDELETED.getSuccess());
        r.setCode(ResultCodeEnum.UNDELETED.getCode());
        r.setMessage(ResultCodeEnum.UNDELETED.getMessage());
        return r;
    }

    //修改失败返回
    public static Result unUpdated() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.UNUPDATED.getSuccess());
        r.setCode(ResultCodeEnum.UNUPDATED.getCode());
        r.setMessage(ResultCodeEnum.UNUPDATED.getMessage());
        return r;
    }

    //未授权返回
    public static Result unauthorized() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.UNAUTHORIZED.getSuccess());
        r.setCode(ResultCodeEnum.UNAUTHORIZED.getCode());
        r.setMessage(ResultCodeEnum.UNAUTHORIZED.getMessage());
        return r;
    }

    //被禁止访问返回
    public static Result forbidden() {
        Result r = new Result();
        r.setSuccess(ResultCodeEnum.FORBIDDEN.getSuccess());
        r.setCode(ResultCodeEnum.FORBIDDEN.getCode());
        r.setMessage(ResultCodeEnum.FORBIDDEN.getMessage());
        return r;
    }

    //设置返回结果
    public static Result setResult(ResultCodeEnum resultCodeEnum) {
        Result r = new Result();
        r.setSuccess(resultCodeEnum.getSuccess());
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());
        return r;
    }

    //设置success状态
    public Result success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    //设置message内容
    public Result message(String message) {
        this.setMessage(message);
        return this;
    }

    //设置code
    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    //设值数据
    public Result data(Object obj) {
        this.setData(obj);
        return this;
    }
}
